package com.lyz.dz.des.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.lyz.dz.des.config.ValidationGroups;

import com.lyz.dz.des.entity.AdverEntity;
import com.lyz.dz.des.pojo.status;
import com.lyz.dz.des.repository.impl.AdverRepositoryImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.validation.constraints.NotNull;

/**
 * <p>
 * 广告表 前端控制器
 * </p>
 *
 * @author <a href="https://fengwenyi.com">Erwin Feng</a>
 * @since 2021-08-21
 */
@RestController
@RequestMapping("/adverEntity")
public class AdverController extends baseController {
    @Autowired
    AdverRepositoryImpl advertServiceImpl;


    /**
     * 获得广告列表
     *
     * @param pageCurrent 当前页面
     * @param pageSize    页面大小
     * @return {@link String}
     * @throws JsonProcessingException json处理异常
     */
    @GetMapping(value = "/adver/list")
    public String getAdvertList(@NotNull Integer pageCurrent, @NotNull Integer pageSize) throws JsonProcessingException {
        Page<AdverEntity> page = advertServiceImpl.page(new Page<>(pageCurrent, pageSize));
        if (page.getRecords().isEmpty()){
            return  this.error(status.ServerCode_error,status.Server_no_data);
        }else {
            return this.success(page);
        }
    }

    @GetMapping(value = "/adver/search")
    public String searchAdvert(@NotNull Integer pageCurrent,@NotNull Integer pageSize, AdverEntity adver) throws JsonProcessingException {
        QueryWrapper<AdverEntity> adverQueryWrapper = new QueryWrapper<>(adver);
        Page<AdverEntity> page = advertServiceImpl.page(new Page<>(pageCurrent, pageSize), adverQueryWrapper);
        if (page.getRecords().isEmpty()){
            return this.error(status.ServerCode_error,status.Server_no_data);
        }else {
            return  this.success(page);
        }

    }
    /**
     * 得到广告通过id列表
     *
     * @param id id
     * @return {@link String}
     * @throws JsonProcessingException json处理异常
     */
    @GetMapping("/adver/{id}")
    public String getAdverListById(@NotNull @PathVariable("id") Integer id) throws JsonProcessingException {
        AdverEntity byId = advertServiceImpl.getById(id);
        if (byId == null){
            return  this.error(status.ServerCode_error,status.Server_no_data);
        }else {
            return  this.success(byId);
        }
    }

    /**
     * 保存广告
     *
     * @param adver  广告
     * @return {@link String}
     * @throws JsonProcessingException json处理异常
     */
    @PostMapping("/adver")
    public String saveAdvert( @Validated(value = ValidationGroups.Save.class) AdverEntity  adver) throws JsonProcessingException{
        boolean save = advertServiceImpl.save(adver);
        if (save){
            return this.success(null);
        }else{
            return  this.error(status.ServerCode_error,status.Operation_faild);
        }
    }

    /**
     * @param id id
     * @return {@link String}
     * @throws JsonProcessingException json处理异常
     */
    @DeleteMapping("/adver/{id}")
    public String deletAdvert(@PathVariable("id") String id) throws JsonProcessingException{
        boolean b = advertServiceImpl.removeById(id);
        if (b){
            return this.success(null);
        }else {
            return  this.error(status.ServerCode_error,status.Operation_faild);
        }
    }

    /**
     * 更新广告
     *
     * @param adver  广告
     * @return {@link String}
     * @throws JsonProcessingException json处理异常
     */
    @PutMapping("/adver")
    public String updateAdvert(AdverEntity adver) throws JsonProcessingException{
        boolean b = advertServiceImpl.updateById(adver);
        if (b){
            return this.success(null);
        }else {
            return  this.error(status.ServerCode_error,status.Operation_faild);
        }
    }

}

